import 'package:get/get.dart';
import 'package:wan_android_flutter/common/network/model/knowledges_model.dart';

import '../../../common/base/base_controller.dart';
import '../../../common/network/http_util.dart';
import '../../../common/network/json_parse.dart';
import 'knowledge_system_state.dart';

class KnowledgeSystemLogic extends BaseController {
  final KnowledgeSystemState state = KnowledgeSystemState();

  @override
  void onLoadData() {
    super.onLoadData();
    loadKnowledges();
  }

  Future<void> loadKnowledges() async {
    final response = await httpUtil.get("/tree/json");
    if (response.statusCode == 200 && response.data is Map<String, dynamic>) {
      final knowledgesModel = JsonParse.fromJsonMap<KnowledgesModel>(response.data);
      if (knowledgesModel?.data?.isNotEmpty == true) {
        state.knowledges.assignAll(knowledgesModel!.data!);
      }
    }
  }
}
